if(length(tryCatch(list.dirs(path="~/Library/CloudStorage/Dropbox/Robots and Trade in Services"),error=function(e) e))==0){
  base.path<-"~/Dropbox/Robots and Trade in Services/"
} else {
  base.path<-"~/Library/CloudStorage/Dropbox/Robots and Trade in Services/"
}

#Load data and functions
source(paste0(base.path,"Drafts/Replication/06 Functions and Processing -- Exp 2.R"))

#############
## TABLE 3 ##
#############

df.raw %>% 
  group_by(treatment_foreign_domestic=ifelse(tmt.foreign==1,"Foreign","Domestic")) %>%
  summarize(val_mean=mean(diff.btw23,na.rm=TRUE),
            val_var=var(diff.btw23,na.rm=TRUE),
            regs_mean=mean(ifelse(treatment==1,regs2,regs3),na.rm=TRUE),
            benefits_mean=mean(ifelse(treatment==1,benefits2,benefits3),na.rm=TRUE),
            num=n()
  ) %>%
  ungroup() %>% 
  mutate(se=sqrt(val_var/num)) %>%
  mutate(contents=paste0("\\makecell{ \\\\ \\footnotesize Policy: $", round(regs_mean,1),"$ \\\\ \\footnotesize Transfers: $",round(benefits_mean,1),"$ \\\\ \\\\ \\normalsize \\textbf{Difference: $",round(val_mean,1),"$} \\\\ \\footnotesize 95\\% Conf. Int. $[",round(val_mean-qnorm(0.975)*se,1),", ",round(val_mean+qnorm(0.975)*se,1),"]$}")) %>% 
  mutate(treatment_foreign_domestic=factor(treatment_foreign_domestic,levels=c("Foreign","Domestic"))) %>%
  select(treatment_foreign_domestic,contents) %>%
  tibble::add_row(treatment_foreign_domestic="",contents="\\makecell{\\textbf{Automation} }",.before=1) %>%
  mutate(treatment_foreign_domestic=paste0("\\textbf{",treatment_foreign_domestic,"}")) %>%
  xtable::xtable(digits=2,label="tab:followup-diffs",caption="Mean differences in policy response minus transfers, by treatment condition.",align=c("|c|c|c|")) %>%
  xtable::print.xtable(comment = FALSE,include.rownames = FALSE,include.colnames = FALSE,sanitize.text.function = function(x) x,hline.after=c(0,1,2,3))
